package webInterface;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.mortbay.util.ajax.Continuation;

import os_listener.ServerUpdates;

import serverManager.ConnectionsKeeper;

import db_manager.Manager;

/**
 * this class handles the sharing dir action
 */
public class UserToShare extends HttpServlet
{
	/*
	 * this function handles the sharing dir action
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		// getting needed parameters
		String oldUserName = session.getAttribute("userName").toString();
		String dirName = session.getAttribute("dirName").toString();
		String userName = request.getParameter("UserName").toString();
		File sessionFile = new File("C:\\Program Files\\m&l_dropbox_server\\hibernate.cfg.xml");
		SessionFactory sessionFactory = new Configuration().configure(sessionFile).buildSessionFactory();
		// creating a session for DB connection
		Session session2 = sessionFactory.openSession();
		PrintWriter out = response.getWriter();
		boolean isExist = Manager.getInstance().isUserExist(userName, session2);
		session2.flush();
		//the case the user exist
		if (isExist)
		{
			Manager manager = Manager.getInstance();	
			// creating a session for DB connection
			Session session3 = sessionFactory.openSession();
			// find this user id
			long user_id = manager.checkUserId(session3, userName);
			session3.flush();
			ConnectionsKeeper ck = ConnectionsKeeper.getInstance();
			ServerUpdates newest_su = new ServerUpdates();
			// update this instance of serverUpdates
			newest_su.setUpdate("sharing folder");
			newest_su.setDir_name(dirName);
			newest_su.setCc_user(user_id);
			newest_su.setOld_user_name(oldUserName);
			ck.setNewest_su(newest_su);
			// get this user continuation
			Continuation cc = ck.getConnection(user_id);
			// the case that the user is currently logged on to it's client program
			if(cc != null)
			{
				// resuming the connection with the user
				cc.resume();
				// entering the dir into the DB with the new user
				manager.share_dir(oldUserName, dirName, user_id);
				// creating a "you have shared successfully" form
				out.println("<html>");
				out.println("<head>");
				out.println("<title>Servlet Error</title>");
				out.println("</head>");
				out.println("<body>");
				out.println("<form action=\"webInterface.SharingDir\" method=\"POST\"> \n you have shared this folder \n <br> <input type=\"submit\" value=\"Return to Share\"> \n </form> \n");
				out.println("</body>");
				out.println("</html>"); 
			}
			// the case that the user is currently logged on to it's client program
			else
			{
				// creating a "user doesn't logged on" form
				out.println("<html>");
				out.println("<head>");
				out.println("<title>Servlet Error</title>");
				out.println("</head>");
				out.println("<body>");
				out.println("<form action=\"webInterface.SharingDir\" method=\"POST\"> \n This user is not connected to the server at the moment \n <br> <input type=\"submit\" value=\"Return to Share\"> \n </form> \n");
				out.println("</body>");
				out.println("</html>");  
			}
		}
		// the case the user doesn't exist
		else
		{
			// creating a "user doesn't exist" form
			out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Error</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<form action=\"webInterface.SharingDir\" method=\"POST\"> \n This user name doesn't exist \n <br> <input type=\"submit\" value=\"Return to Share\"> \n </form> \n");
            out.println("</body>");
            out.println("</html>");   
		}
		out.close();
	}
}
